package com.qf.controller;

import com.qf.pojo.Admin;
import com.qf.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 张鑫
 * @date 2020/10/19 19:05
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("tologin")
    public String index() {
        return "/behind/login.jsp";
    }

    // 管理员登录
    @ResponseBody
    @RequestMapping("login")
    public String adminLogin(Admin data, HttpSession session) {

        List<Admin> adminCheck = adminService.findAdmin(data);

        if (adminCheck != null) {
            String username = data.getUsername();
            session.setAttribute("userName", username);
            return "success";
        } else {
            return "no";
        }
    }


//    // 管理员退出
//    @RequestMapping("exit")
//    public String adminExit(HttpServletRequest request, HttpServletResponse response) {
//        HttpSession session = request.getSession(false);
//        session.invalidate();
//        Cookie jsessionid = new Cookie("JSESSIONID", "");
//        jsessionid.setMaxAge(0);
//        response.addCookie(jsessionid);
//
//        //return "/behind/login.jsp";
//        return "redirect:/user/show";
//    }

    // 管理员退出
    @RequestMapping("exit")
    public String adminExit(HttpSession session) {

        session.invalidate();

        //return "/behind/login.jsp";
        return "redirect:/user/show";
    }



}
